﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text;
using System.Windows.Forms;
using AsPlugInManager;
using AsLib;

namespace NNLInterface
{
    public partial class frmProductManCount : NNLInterface.frmBaseBasic
    {
        
        #region 初始化

        string strSQLHead = " SELECT RrsourceCode,ItemCode,ManCount,itemid,opecount from ManCount"; 
  
        string DeptNo = "";
        
        string strStartTime = "";
        string strEndTime = "";

        DataTable dtData;

        public frmProductManCount()
        {
            InitializeComponent();
        }



        public frmProductManCount(IASPArgList argList)  
        {
            InitializeComponent();
            common.agrs = (ASPArgList)argList;
            this.CmdImport.Caption = "统计";
            this.SearchButtonVisibility = false;
         
        }

        void frmProductManCount_Load(object sender, EventArgs e)
        {
            //如果当前工程下的用户名为空，则提示输入
            if (common._userid == "")
            {
                frmLogon frmLog = new frmLogon();
                frmLog.ShowDialog();

                if (frmLog.DialogResult == DialogResult.OK)
                {
                    frmLog.Dispose();
                    frmLog = null;
                }
                else
                {
                    this.Close();
                    return;
                }

            }
            IniDataSource();
        }

        private void frmProductManCount_ToolButtonClick(object sender, string e)
        {
            try
            {
                switch (e)
                {
                    case "import":
                        if (!CheckData()) return;
                        if (DevExpress.XtraEditors.XtraMessageBox.Show("确定要进行切换次数统计吗(Y/N)?", common.sysParameter.MessageInf, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No) return ;
                        ShowData();
                        break;
                    case "excel":
                        subExportXLS();
                        break;
                    case "print":
                        common.PrintView(this.ListGrid, "工单数据");
                        break;
                    case "exit":
                        common.SaveFormLayOut(this, this.layOut, this.grpHead);
                        common.SaveLayout(this.ListView);
                        this.Dispose();
                        break;
                    default:
                        {
                            break;
                        }
                }


            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private bool CheckData()
        {
            if (this.dteFrom.Text == "")
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请输入一个正确的起始时间", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.dteFrom.Focus();
                return false;
            }

            if (this.dteTo.Text == "")
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请输入一个正确的截止时间", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.dteTo.Focus();
                return false;
            }

            return true;
        }

        private void ShowData()
        {
            Cursor currentCursor = Cursor.Current;
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                //取得唯一标识
                string itemID = accessdb.GetField("select CONVERT(NVARCHAR(30),GETDATE(),121)").ToString();

                //取得日境界时间
                string dayStartTime = common.pro.DayStartTime.ToString("HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
                int hour = Convert.ToInt32(dayStartTime.Substring(0, 2));
                int minute = Convert.ToInt32(dayStartTime.Substring(3, 2));
                int second = Convert.ToInt16(dayStartTime.Substring(6, 2));
                string strSQL = "EXEC pro_ProductManCount" + common._userid
                             + " '" + dteFrom.Text + "','"
                             + dteTo.Text + "','"
                             + dayStartTime.ToString() + "',"
                             + hour.ToString() + ","
                             + minute.ToString() + ","
                             + second.ToString() + ",'"
                             + itemID + "',''";

                if (!accessdb.RsChange(strSQL)) return;
                strSQL = strSQLHead + " where itemid='" + itemID + "'";
                dtData = accessdb.GetRecord(strSQL).ADODataRst;
                common.SubShowList(dtData, this.ListGrid, this.ListView, false);
                common.subIniGrid(this.ListView, common.enumGridStyle.ViewStyle);

            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);

                return ;
            }
            finally
            {
                Cursor.Current = currentCursor;
            }
           
        }

        private void subExportXLS()
        {
            try
            {
                if (!common.CheckExistsData(this.ListView)) return;
                common.ExportExcel(this.ListView);
                return;
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        #endregion
        private void IniDataSource()
        {
            //设定日期值
            this.txtUser.Text = common._userid;
            this.txtUserName.Text = common._username;
            this.txtDeptNo.Text = common._DeptNo;
            this.txtDeptName.Text = common._DeptName;


     

            string strSQL = strSQLHead;
            dtData = accessdb.GetRecord(strSQL + " where 1=2").ADODataRst;
            common.SubShowList(dtData, this.ListGrid, this.ListView, false);
            common.subIniGrid(this.ListView, common.enumGridStyle.ViewStyle);
            common.ReStoreLayOut(this.ListView);
            common.GetResourceString(this, this.ListView);
            common.RestoreFormLayOut(this, this.layOut, this.grpHead);
            this.SetToolVisible(common.ToolVisible.ImportData);
            this.logButtonVisibility = false;

        }



        //切换用户
        private void txtChange_Click(object sender, EventArgs e)
        {
            frmLogon frmLog = new frmLogon();
            frmLog.ShowDialog();

            if (frmLog.DialogResult == DialogResult.OK)
            {
                IniDataSource();
            }

            frmLog.Dispose();
            frmLog = null;
        }

        private void frmProductManCount_FormClosing(object sender, FormClosingEventArgs e)
        {
            serial.SerializeBinary(common.sysParameter, Application.StartupPath + @"\NNLInterface.dat");
            common._its_accessdb = null;
            common.pro = null;
            GC.Collect();
        }
    }
}
